﻿Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Script.Services
Imports System.Web.Services.Protocols
Imports VSModel
Imports System.Data.Objects
Imports Cls_Common
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class wsAutoComplete
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetListAutoCompleteProduct(ByVal strSrc As String) As List(Of String)
        Dim json As New List(Of String)
        Dim strConnect As String = ""
        Using data As New VSEntities
            'Load du lieu ung voi ky tu nhap vao

            Dim p = (From m In data.Products Where m.Code.Contains(strSrc) Or m.Title.Contains(strSrc) Or m.QueryString.Contains(strSrc) _
                     Order By m.Title Ascending Select m).ToList

            'Export du lieu sang kieu du lieu json
            If Not p Is Nothing AndAlso p.Count > 0 Then
                For i As Integer = 0 To p.Count - 1
                    json.Add(p(i).Code.ToString & " - " & p(i).Title.ToString)
                Next
            End If
        End Using
        Return json.ToList()
    End Function

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetListAutoCompleteNganhNghe(ByVal strSrc As String) As List(Of String)
        Dim json As New List(Of String)
        Dim strConnect As String = ""
        Using data As New VSEntities
            'Load du lieu ung voi ky tu nhap vao

            'Dim p = (From m In data.DanhMucNganhNghes _
            '         Where (m.Code.Contains(strSrc) Or m.Name.Contains(strSrc)) And m.CAP4 IsNot Nothing _
            '         Order By m.Name Ascending Select m).ToList
            Dim p = (From m In data.DanhMucNganhNghes _
                     Where (m.Code.Contains(strSrc) Or m.Name.Contains(strSrc)) _
                     Order By m.Name Ascending Select m).ToList

            'Export du lieu sang kieu du lieu json
            If Not p Is Nothing AndAlso p.Count > 0 Then
                For i As Integer = 0 To p.Count - 1
                    json.Add(p(i).Code.ToString & " - " & p(i).Name.ToString)
                Next
            End If
        End Using
        Return json.ToList()
    End Function

    <WebMethod()> _
      <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function SearchProduct(ByVal strSrc As String) As List(Of String)
        Dim json As New List(Of String)
        Dim strConnect As String = ""
        Using data As New VSEntities
            'Load du lieu ung voi ky tu nhap vao
            Dim abc = makeURLFriendly(strSrc)
            Dim p = (From m In data.Products Where m.Title.Contains(strSrc) Or m.QueryString.Contains(abc) Or m.Code.Contains(strSrc) _
                     Order By m.Title Ascending Select m).ToList

            'Export du lieu sang kieu du lieu json
            If Not p Is Nothing AndAlso p.Count > 0 Then
                For i As Integer = 0 To p.Count - 1
                    If Not p(i).Title Is Nothing Then
                        json.Add(p(i).Code.ToString & " - " & p(i).Title.ToString)
                    Else
                        json.Add(p(i).Code.ToString)
                    End If

                Next
            End If
        End Using
        Return json.ToList()
    End Function


    'Generate OrderNumber

    '   <WebMethod(EnableSession:=True)> _
    '<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    '   Public Function GenerateOrderNumber(ByVal sSACode As String) As String
    '       Session("curSA") = sSACode
    '       Dim dicSA As New Dictionary(Of String, String)

    '       'create session lưu trữ lại SACode khi bấm chọn ddlSA  Then
    '       If Not Session("ListGenPO") Is Nothing Then
    '           dicSA = CType(Session("ListGenPO"), Dictionary(Of String, String))
    '           If dicSA.ContainsKey(sSACode) Then
    '               Return dicSA(sSACode)
    '           End If
    '       End If

    '       Dim result As String
    '       Using data As New VSEntities
    '           Dim k = (From a In data.Stores Where a.StoreId = sSACode Select a).FirstOrDefault
    '           'neu ngay hien tai khac ngay luu trong database thi reset sequence = 1
    '           If (k.OrderDate <> Now.Date) Or k.OrderDate Is Nothing Then
    '               k.DailyOrder = 1
    '               k.OrderDate = Now.Date
    '               data.SaveChanges()
    '           Else
    '               k.DailyOrder = k.DailyOrder + 1
    '               data.SaveChanges()
    '           End If
    '           result = k.StoreId.ToString("0#") & "-" & Now.Date.ToString("yyMMdd") & "-" & CType(k.DailyOrder, Integer).ToString("00#")

    '           'lưu vào dic
    '           dicSA(sSACode) = result
    '           Session("ListGenPO") = dicSA

    '       End Using
    '       Return result
    '   End Function

    <WebMethod(EnableSession:=True)>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function getProvince(ByVal ProvinceId As String) As String
        If ProvinceId <> "" Then
            Dim _ProvinceId As Integer = CType(ProvinceId, Integer)
            Using _data As New VSEntities
                Dim k = (From q In _data.Provinces Where q.ParentId = _ProvinceId Order By q.Title Select q.Id, q.Code, q.Title).ToList
                Dim serializer As New JsonSerializer
                serializer.Converters.Add(New JavaScriptDateTimeConverter())
                serializer.NullValueHandling = NullValueHandling.Ignore
                Dim sJSON As String = JsonConvert.SerializeObject(k)
                Return sJSON
            End Using
        End If
        Return ""

    End Function

    <WebMethod(EnableSession:=True)>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function getStatusByStatus(ByVal FunctionId As String) As String
        Dim _FunctionId As Integer = CType(FunctionId, Integer)
        Using _data As New VSEntities
            Dim k = (From q In _data.Status Where q.FunctionId = _FunctionId Order By q.Id, q.Title Select q.StatusId, q.Title).ToList
            Dim serializer As New JsonSerializer
            serializer.Converters.Add(New JavaScriptDateTimeConverter())
            serializer.NullValueHandling = NullValueHandling.Ignore
            Dim sJSON As String = JsonConvert.SerializeObject(k)
            Return sJSON
        End Using

    End Function


    <WebMethod(EnableSession:=True)>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function getActionByFunction(ByVal FunctionId As String) As String
        Dim _FunctionId As Integer = CType(FunctionId, Integer)
        Using _data As New VSEntities
            Dim k = (From q In _data.Actions Where q.FunctionId = _FunctionId Order By q.ActionId, q.Title Select q.ActionId, q.Title).ToList
            Dim serializer As New JsonSerializer
            serializer.Converters.Add(New JavaScriptDateTimeConverter())
            serializer.NullValueHandling = NullValueHandling.Ignore
            Dim sJSON As String = JsonConvert.SerializeObject(k)
            Return sJSON
        End Using

    End Function



End Class